<?php

namespace app\admin\controller;

use app\models\{Admin as model, AdminLogin, Region};
use app\validate\Admin as Validate;
use think\Db;
use think\facade\Debug;

class Admin extends Base
{

    public function index()
    {
        $admin = $this->request->admin;
        $results = (new model())->where('id', '<>', $admin['id'])->order('id desc');
        $data = $this->request->get();
        if (!empty($data['title'])) {
            $results = $results->where('nickname|username', 'like', '%' . $data['title'] . '%');
        }
        $results = $results->paginate(10);
        foreach ($results as $result) {
            $result['cangku'] = config()["config"]['cangku'][$result['type']] ?? '';
        }
        return view(null, [
            'list' => $results,
        ]);
    }

    public function input()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post('data');
            $validate = new Validate();
            if (!$validate->scene('add')->check($data) && empty($data['id'])) {
                return json(['status' => 1, 'msg' => $validate->getError()]);
            }
            if (!$validate->scene('edit')->check($data) && !empty($data['id'])) {
                return json(['status' => 1, 'msg' => $validate->getError()]);
            }
            $haveadmin = (new model())->where('username', $data['username'])->find();
            if (!empty($haveadmin) && $haveadmin['id'] != $data['id']) {
                return json(['status' => 1, 'msg' => '账号名称已被使用']);
            }
            //管理员权限判断
            $data['role'] = implode(',', $data['role'] ?? []);
            if (isset($data['password']) && $data['password']) {
                $entry = $this->get_rand_str();
                $data['entry'] = $entry;
                $data['password'] = md5(md5($data['password']) . md5($entry));
            } else {
                unset($data['password']);
            }
            if ($data['id'] > 0) {
                $res = (new model())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new model())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 0, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new model())->where('id', $id)->find();
            if (!empty($row)) {
                $row['role'] = array_filter(explode(',', $row['role']));
            } else {
                $data['role'] = [];
            }
            $cangku = config()["config"]['cangku'] ?? [];
            return view('', [
                'data' => $row,
                'role' => $this->request->admin['role'],
                'cangku' => $cangku,

            ]);
        }
    }

    public function del()
    {
        $admin_id =  $this->request->post('id');
        $admin = (new model())->where('id', $admin_id)->find();
        if ($admin['is_default'] == 1) {
            return json(['status' => 1, 'msg' => '超级管理员不允许删除']);
        }
        $res = (new model())->where('id', $admin_id)->delete();
        if ($res) {
            return json(['status' => 0, 'msg' => '删除成功']);
        }
        return json(['status' => 1, 'msg' => '操作失败']);
    }

    //登录日志
    public function login_log()
    {
        $data = input('param.');
        $list = (new AdminLogin())->where('uid', $this->request->admin['id'])->order('id DESC');
        if (isset($data['username']) && $data['username']) {
            $list->where('username', $data['username']);
        }
        $lists = $list->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $lists]);
    }
}
